﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

class Bomb : SpriteGameObject
{
    protected bool exploded;
    protected float previousXPosition;

    int maxBounces = 3;
    int bounces = 0;

    public Bomb(Vector2 startPosition, float xSpeed, int layer=0, string id="bomb") : base("Sprites/spr_bomb", layer, id)
    {
        //Set default values
        exploded = false;  
        velocity.X = xSpeed;
        velocity.Y = -1800;
        //Make sure the entered position is the center of the bomb
        position = startPosition - new Vector2(sprite.Width,sprite.Height) / 2;
    }

    public override void Update(GameTime gameTime)
    {
        //Explode if the bomb bounced too often
        if(bounces >= maxBounces)
        {
            Explode();
        }
        //Remove bomb if it left the level
        if (position.Y > (parent as Level).Height || exploded)
        {
            (parent as Level).Remove(this);
            return;
        }
        base.Update(gameTime);
        if(!exploded)
            DoPhysics();
    }

    private void Explode()
    {
        exploded = true;
    }

    private void DoPhysics()
    {
        //Gravity
        velocity.Y += 100;
        this.velocity = new Vector2((int)velocity.X, (int)velocity.Y);

        //Collision with the level.
        HandleCollisions();
    }
    private void HandleCollisions()
    {
        TileField tiles = GameWorld.Find("tiles") as TileField;
        int x_floor = (int)position.X / tiles.CellWidth;
        int y_floor = (int)position.Y / tiles.CellHeight;

        //Loop through required tiles
        for (int y = y_floor - 2; y <= y_floor + 1; ++y)
            for (int x = x_floor - 1; x <= x_floor + 1; ++x)
            {
                TileType tileType = tiles.GetTileType(x, y);
                if (tileType == TileType.Background)
                    continue;
                Tile currentTile = tiles.Get(x, y) as Tile;
                Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight,
                                                        tiles.CellWidth, tiles.CellHeight);
                Rectangle boundingBox = this.BoundingBox;
                boundingBox.Height += 1;
                if (((currentTile != null && !currentTile.CollidesWith(this)) || currentTile == null) && !tileBounds.Intersects(boundingBox))
                    continue;
                Vector2 depth = Collision.CalculateIntersectionDepth(boundingBox, tileBounds);

                //Put the rectangle back in x direction
                if (new Rectangle((int)previousXPosition,boundingBox.Y,boundingBox.Width,boundingBox.Height).Intersects(tileBounds))
                {
                    //The collision is caused by movement in the y direction because even when putting back in x direction there is still collision
                    if (tileType == TileType.Normal || (tileType == TileType.Platform && velocity.Y > 0))
                    {
                        position.Y += depth.Y;
                        velocity.Y *= -0.5f;
                        bounces++;
                    }
                    continue;
                }
                else
                {
                    //Collision is caused by movement in the x direction
                    position.X += depth.X;
                    velocity.X *= 0.5f;
                }
            }
        position = new Vector2((float)Math.Floor(position.X), (float)Math.Floor(position.Y));

        //Save this y-position for the next loop.
        previousXPosition = position.X;
    }

}
